#include "CMainDlg.h"
#include "resource.h"

BEGIN_DLG_MESSAGE_MAP(CMainDlg, CDialogBase)
    ON_DLG_MESSAGE(WM_INITDIALOG, &CMainDlg::OnInitDialog)
    ON_DLG_MESSAGE(WM_CLOSE, &CMainDlg::OnClose)

    ON_DLG_COMMAND(IDC_RADIO_LINE, &CMainDlg::OnCmdLine)
    ON_DLG_COMMAND(IDC_RADIO_RECT, &CMainDlg::OnCmdRect)
    ON_DLG_COMMAND(IDC_RADIO_RECT_FILL, &CMainDlg::OnCmdRectFill)
    ON_DLG_COMMAND(IDC_RADIO_CIRCLE, &CMainDlg::OnCmdCircle)
    ON_DLG_COMMAND(IDC_RADIO_GRID, &CMainDlg::OnCmdGrid)
    ON_DLG_COMMAND(IDC_RADIO_GRID_FILL, &CMainDlg::OnCmdGridFill)
    ON_DLG_COMMAND(IDC_RADIO_PEN, &CMainDlg::OnCmdPen)
    ON_DLG_COMMAND(IDC_CHECK_GRID, &CMainDlg::OnCmdCheckGrid)
    ON_DLG_COMMAND(IDC_CHECK_TIME, &CMainDlg::OnCmdCheckTime)

    ON_DLG_COMMAND(IDC_BUTTON_RESET, &CMainDlg::OnCmdBtnReset)
    ON_DLG_COMMAND(IDC_BUTTON_UNDO, &CMainDlg::OnCmdBtnUnDo)
    ON_DLG_COMMAND(IDC_BUTTON_REDO, &CMainDlg::OnCmdBtnReDo)
    ON_DLG_COMMAND(IDC_BUTTON_SET_COUNT, &CMainDlg::OnCmdBtnSetCount)
    ON_DLG_COMMAND(IDC_RADIO_BITMAP, &CMainDlg::OnCmdBtnBitmap)
    ON_DLG_COMMAND(IDC_RADIO_GDI, &CMainDlg::OnCmdBtnGDI)
    ON_DLG_COMMAND(ID_FILE_EXIT, &CMainDlg::OnCmdMenuExit)

END_DLG_MESSAGE_MAP()

LRESULT CMainDlg::OnInitDialog(WPARAM wParam, LPARAM lParam)
{
    m_hWndPaint.Register(_T("FC_Paint"));
    m_hWndPaint.Create(m_hWnd, _T(""), SW_SHOW, WS_CHILD, 0, 8, 8, 768, 720, FALSE);


    ::CheckDlgButton(m_hWnd, IDC_RADIO_LINE, BST_CHECKED);
    ::CheckDlgButton(m_hWnd, IDC_CHECK_GRID, BST_CHECKED);
    ::CheckDlgButton(m_hWnd, IDC_CHECK_TIME, BST_CHECKED);


    ::CheckDlgButton(m_hWnd, IDC_RADIO_BITMAP, BST_CHECKED);

    PostMessage(WM_COMMAND, IDC_RADIO_LINE, 0);
    PostMessage(WM_COMMAND, IDC_CHECK_GRID, 0);
    PostMessage(WM_COMMAND, IDC_CHECK_TIME, 0);

    SetDlgItemInt(m_hWnd, IDC_EDIT_COUNT, 1, FALSE);

    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnClose(WPARAM wParam, LPARAM lParam)
{
    return (LRESULT)FALSE;
}

LRESULT CMainDlg::OnCmdLine(WPARAM wParam, LPARAM lParam)
{
    m_hWndPaint.SetDrawMode(DrawType::eDTLine);
    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnCmdRect(WPARAM wParam, LPARAM lParam)
{
    m_hWndPaint.SetDrawMode(DrawType::eDTRect);
    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnCmdRectFill(WPARAM wParam, LPARAM lParam)
{
    m_hWndPaint.SetDrawMode(DrawType::eDTFillRect);
    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnCmdCircle(WPARAM wParam, LPARAM lParam)
{
    m_hWndPaint.SetDrawMode(DrawType::eDTCircle);
    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnCmdGrid(WPARAM wParam, LPARAM lParam)
{
    m_hWndPaint.SetDrawMode(DrawType::eDTGrid);
    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnCmdGridFill(WPARAM wParam, LPARAM lParam)
{
    m_hWndPaint.SetDrawMode(DrawType::eDTFillGrid);
    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnCmdPen(WPARAM wParam, LPARAM lParam)
{
    m_hWndPaint.SetDrawMode(DrawType::eDTPoints);
    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnCmdCheckGrid(WPARAM wParam, LPARAM lParam)
{
    bool bCheck = ::IsDlgButtonChecked(m_hWnd, wParam);
    m_hWndPaint.SetDrawGrid(bCheck);
    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnCmdCheckTime(WPARAM wParam, LPARAM lParam)
{
    bool bCheck = ::IsDlgButtonChecked(m_hWnd, wParam);
    m_hWndPaint.SetDrawTime(bCheck);
    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnCmdBtnReset(WPARAM wParam, LPARAM lParam)
{
    m_hWndPaint.Reset();
    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnCmdBtnUnDo(WPARAM wParam, LPARAM lParam)
{
    m_hWndPaint.UnDo();
    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnCmdBtnReDo(WPARAM wParam, LPARAM lParam)
{
    m_hWndPaint.ReDo();
    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnCmdBtnSetCount(WPARAM wParam, LPARAM lParam)
{
    UINT uCount = GetDlgItemInt(m_hWnd, IDC_EDIT_COUNT, nullptr, FALSE);
    if (uCount <= 0)
    {
        uCount = 1;
        SetDlgItemInt(m_hWnd, IDC_EDIT_COUNT, uCount, FALSE);
    }
    m_hWndPaint.SetCount(uCount);
    m_hWndPaint.DrawItemList();
    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnCmdBtnBitmap(WPARAM wParam, LPARAM lParam)
{
    m_hWndPaint.SetMode(false);
    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnCmdBtnGDI(WPARAM wParam, LPARAM lParam)
{
    m_hWndPaint.SetMode(true);
    return (LRESULT)TRUE;
}

LRESULT CMainDlg::OnCmdMenuExit(WPARAM wParam, LPARAM lParam)
{
    PostMessage(WM_CLOSE, 0, 0);
    return (LRESULT)TRUE;
}